package com.hyf.serviceedu.controller;


import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyf.commonutils.fruit;
import com.hyf.serviceedu.entity.EduCourse;
import com.hyf.serviceedu.entity.condition.CoursePublishVo;
import com.hyf.serviceedu.entity.condition.CourseQuery;
import com.hyf.serviceedu.entity.condition.CourseinfoVo;
import com.hyf.serviceedu.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-04-21
 */
@Api(description="课程")
@RestController
@RequestMapping("/serviceedu/course")
//@CrossOrigin // 解决跨域
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;// 注入 service课程管理



    // 课程列表
    // 条件查询带分页 TODO
    // 单纯查询课程列表
    @GetMapping
    public fruit getCouresList() {
        List<EduCourse> list=eduCourseService.list(null);

        return fruit.ok().data("list",list);
    }






    // ************************************


// 条件查询带分页 教程课件中所用的方法
    @ApiOperation(value = "分页课程列表")
    @GetMapping("{page}/{limit}")
    public fruit pageQuery(
         @ApiParam(name = "page", value = "当前页码", required = true)
         @PathVariable Long page,
        @ApiParam(name = "limit", value = "每页记录数", required = true)
        @PathVariable Long limit,
        @ApiParam(name = "courseQuery", value = "查询对象", required = false)
                 CourseQuery courseQuery){
        Page<EduCourse> pageParam = new Page<>(page, limit);
        eduCourseService.pageQuery(pageParam, courseQuery);
        List<EduCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return  fruit.ok().data("total", total).data("rows", records);
    }


    //********************************



    //******************************





    //*****--------------











    // 添加课程基本信息的方法
    @ApiOperation(value = "新增课程")
    @PostMapping("addCourseinfo")
    public fruit addCourseinfo(
            @ApiParam(name = "courseinfovo", value = "课程基本信息", required = true)
            @RequestBody CourseinfoVo courseinfovo){
        // 返回添加之后的课程id
        String courseId= eduCourseService.saveCourseinfo(courseinfovo);

        if(!StringUtils.isEmpty(courseId)){
            return fruit.ok().data("courseId", courseId);
        }else{
            return fruit.error().message("保存失败");
        }

    }


    // 根据课程id 查询课程的基本信息
    @ApiOperation(value = "根据ID查询章节")
    @GetMapping("getCourseinfo/{courseId}")
    public fruit getCourseinfo(
            @ApiParam(name = "courseId", value = "章节ID", required = true)
            @PathVariable String courseId){
        CourseinfoVo courseinfovo= eduCourseService.getCourseInfo(courseId);
        return fruit.ok().data("courseinfovo",courseinfovo);

    }
    // 修改课程信息
    @ApiOperation(value = "根据ID修改章节")
    @PostMapping("updateCourseinfo")
    public fruit updateCourseinfo(@RequestBody CourseinfoVo courseinfovo){
        eduCourseService.updateCourseinfo(courseinfovo);

        return fruit.ok();

    }

    /// 根据课程 id 查询 课程确认信息
    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("getPublishCourseinfo/{id}")
    public fruit getPublishCourseinfo(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id){
        CoursePublishVo courseinfovo=  eduCourseService.publishCourseinfo(id);
        return fruit.ok().data("publishCourse",courseinfovo);
    }


// 课程最终发布
    // 修改课程状态
    @PostMapping("publishCourse/{id}")
    public fruit publishCourse( @PathVariable String id){
        EduCourse course=new EduCourse();
        course.setId(id);
        course.setStatus("Normal");// 设置课程发布的状态
        eduCourseService.updateById(course);
        return fruit.ok();

    }

// 删除课程 这个是物理删除不是 逻辑删除
@ApiOperation(value = "根据ID删除课程")
    @DeleteMapping("{courseId}")
    public fruit deleteCourse(
        @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId){

        eduCourseService.removeCourseById(courseId);

        return fruit.ok();
    }


    //**************************课件中的方法
  /*  @ApiOperation(value = "根据ID删除课程")
    @DeleteMapping("{id}")
    public fruit removeById(
@ApiParam(name = "id", value = "课程ID", required = true)
        @PathVariable String id){
        boolean result = eduCourseService.removeCourseById(id);
        if(result){
            return fruit.ok();
        }else{
            return fruit.error().message("删除失败");
        }
    }*/
// ***********************************************
}

